import 'package:flutter/material.dart';

class RadioDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RadioStateDemo();
  }
}

class RadioStateDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return RadioState();
  }
}

class RadioState extends State<RadioStateDemo> {
  //权组值
  int _radioGroupA = 0;

  int _radioGroupB = 0;

  void _handleRadioValueChange(int value) {
    setState(() {
      //权组值和value相等，才会被选中
      _radioGroupA = value;
    });
  }

  void _handleRadioListValueChange(int value) {
    setState(() {
      //权组值和value相等，才会被选中
      _radioGroupB = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RadioDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Radio(
                  value: 0,
                  groupValue: _radioGroupA,
                  onChanged: _handleRadioValueChange,
                  activeColor: Colors.green,
                ),
                Radio(
                  value: 1,
                  groupValue: _radioGroupA,
                  onChanged: _handleRadioValueChange,
                  activeColor: Colors.green,
                ),
              ],
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RadioListTile(
                  value: 0,
                  title: Text('Option A'),
                  subtitle: Text('descriptionA'),
                  secondary: Icon(Icons.filter_1),
                  //内容是否和选中状态一起激活
                  selected: _radioGroupB == 0,
                  groupValue: _radioGroupB,
                  activeColor: Colors.lightBlue,
                  onChanged: _handleRadioListValueChange,
                ),
                RadioListTile(
                  value: 1,
                  title: Text('Option B'),
                  subtitle: Text('descriptionB'),
                  secondary: Icon(Icons.filter_2),
                  //内容是否和选中状态一起激活
                  selected: _radioGroupB == 1,
                  activeColor: Colors.lightBlue,
                  groupValue: _radioGroupB,
                  onChanged: _handleRadioListValueChange,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
